<style>
*{
    padding: 0;
    margin: 0;
}
body{
    background-color: #000;
}
div{
    width: 300px;
    border: 1px solid white;
}
span{
    display: inline-block;
    width: 10px;
    height: 20px;
    background-color: red;
}
</style>
<body>
<!-- <img src="./imgs/star.gif" alt=""> -->
<div>
    
</div>
</body>
<script>
//获取点击事件

setInterval(function(){
// document.body.onclick = function(evt){
    //写事件函数
    // var e = evt || window.event
    //建一个img标签
    var imgobj = document.createElement('img')
    //添加src属性
    imgobj.src = './imgs/star.gif'
    // console.log(imgobj)
    document.body.appendChild(imgobj)
    //随机设置星星的宽高
    var w = Math.floor(Math.random() * (50-20+1)+40)
    var h = Math.floor(Math.random() * (50-20+1)+40)
    imgobj.width = w
    imgobj.height = h
    imgobj.style.position = 'absolute'
    var wid = window.innerWidth || document.documentElement.clientWidth-w
    var hei = window.innerHeight || document.documentElement.clientHeight-h
    var w1 = Math.floor(Math.random() * (wid-0+1)+0)
    var h1 = Math.floor(Math.random() * (hei-0+1)+0)
    imgobj.style.left = w1 + 'px'
    imgobj.style.top = h1 + 'px'
},1000)
t=  setInterval(function(){
    var spanOj = document.createElement('span')
    document.querySelector('div').appendChild(spanOj)
    var sp = document.querySelectorAll('span')
    if(sp.length>30){
        alert('游戏结束，重新来一局')
        window.location.reload()  
          clearInterval(t)
        }
},400)
//事件函数
document.querySelector('body').onclick = function(evt){
    var e = evt || window.event
    //获取当前标签
    var target = e.target 
    if(target.nodeName == 'IMG'){
        this.removeChild(target)
    } 
    var spanOj = document.querySelector('div').lastChild
    console.log(spanOj)
    document.querySelector('div').removeChild(spanOj)
}
     
</script>